package com.controller;

import com.po.Alltype;
import com.po.Coach;
import com.po.Course;
import com.service.CourseService;
import com.util.DataFormat;
import com.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阳幸子 on 2018/5/8.
 */
@Controller
public class EventsController {
    @Autowired
    private CourseService courseService;

    @RequestMapping("/event_manager")
    public String course_manager(int page,Model model){
        List<Alltype> listType=courseService.selectAlltype();
        List<Coach> listCoach=courseService.selectAllCoach();
        int count=courseService.countEvent();
        int pageSize=0;
        if(count%6==0){
            pageSize=count/6;
        }else{
            pageSize=count/6+1;
        }
        if(page>0){
            page=(page-1)*6;
        }
        List<Course> events=courseService.selectEventByPage(page);
        List<com.vo.Course> listEvent=new ArrayList<>();
        for (int i = 0; i < events.size(); i++) {
            Course course =  events.get(i);
            com.vo.Course vcourse=new com.vo.Course();
            cloneCourse(course,vcourse);
            Alltype type=courseService.selectTypeById(course.getCourse_type_id());

            Coach coach=courseService.selectCoachById(course.getCoach_id());
            vcourse.setCoach(coach);
            vcourse.setCourseType(type);
            listEvent.add(vcourse);
        }
        model.addAttribute("countEvent",count);
        model.addAttribute("listEvent",listEvent);
        model.addAttribute("pageSize",pageSize);

        return "/manager/events_manager";
    }

    public static void cloneCourse(Course course,com.vo.Course vcourse){
        vcourse.setCtype(course.getCtype());
        vcourse.setCourseDesc(course.getCourse_desc());
        vcourse.setEndTime(course.getEnd_time());
        vcourse.setStartTime(course.getStart_time());
        vcourse.setId(course.getId());
        vcourse.setLikeCount(course.getLike_count());
        vcourse.setCourseName(course.getCourse_name());

    }
    @RequestMapping(value = "/add_event")
    public String add_course(Model model){
        List<Alltype> listType=courseService.selectAlltype();
        List<Coach> listCoach=courseService.selectAllCoach();

        model.addAttribute("listType",listType);
        model.addAttribute("listCoach",listCoach);
        return "/manager/add_event";
    }


    @RequestMapping(value = "/add_eventForm",method = RequestMethod.POST)
    public String addCourseForm(CourseVo courseVo){
        Course course= DataFormat.voFormat(courseVo);
        course.setCtype("event");
        courseService.insertCourse(course);
        System.out.println(course);
        return "redirect:/event_manager?page=1";
    }

    @RequestMapping(value = "/edit_event")
    public String edit_course(Model model,long id){
        List<Alltype> listType=courseService.selectAlltype();
        List<Coach> listCoach=courseService.selectAllCoach();
        Course event=courseService.selectCourseById(id);
        model.addAttribute("event",event);

        model.addAttribute("listType",listType);
        model.addAttribute("listCoach",listCoach);
        return "/manager/edit_course";
    }

    @RequestMapping(value = "/edit_eventForm",method = RequestMethod.POST)
    public String addCourseForm(CourseVo courseVo,Model model,long id){
        Course course= DataFormat.voFormat(courseVo);
        course.setId(id);

        courseService.updateCourse(course);
        System.out.println(course);
        return "redirect:/event_manager?page=1";
    }
    @RequestMapping("/selectEvent")
    public String selectEvent(long id,Model model){
        List<Alltype> listType=courseService.selectAlltype();
        List<Coach> listCoach=courseService.selectAllCoach();
        Course course=courseService.selectCourseById(id);
        com.vo.Course vcourse=new com.vo.Course();
        cloneCourse(course,vcourse);
        Alltype type=courseService.selectTypeById(course.getCourse_type_id());

        Coach coach=courseService.selectCoachById(course.getCoach_id());
        vcourse.setCoach(coach);
        vcourse.setCourseType(type);
        model.addAttribute("course",vcourse);
        return "/manager/event_detail";
    }

    @RequestMapping("/del_Event")
    public String delEvent(long id){
        courseService.delCourse(id);
        return "";
    }

}
